package com.snake.rabbitmq.topic.producer;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.snake.rabbitmq.utils.MQConnectionUtils;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 主题交换机
 * Topic exchange（主题交换机）队列通过路由键绑定到交换机上，然后，交换机根据消息里的路由值，将消息路由给一个或多个绑定队列
 * @author : jjbin
 * @date : 14:22 2021/5/25
 */
public class TopicProducer {

    //交换机名称
    private static final String DESTINATION_NAME = "my_topic_estination";

    public static void main(String[] args) throws IOException, TimeoutException {
        // 1. 建立mq连接
        Connection connection = MQConnectionUtils.newConnection();
        // 2. 创建通道
        Channel channel = connection.createChannel();
        // 3. 生产者绑定交换机 参数 1 交换机名称 参数2 交换机类型
        channel.exchangeDeclare(DESTINATION_NAME,"topic");
        String routingKey = "log.email.sms";
        // 4.创建消息
        String msg = "my_topic_estination hahhah" + routingKey;
        System.out.println("生产者投递消息:" + msg);
        // 5.发送消息 my_routing_estination  routing-key= email
        channel.basicPublish(DESTINATION_NAME,routingKey,null,msg.getBytes());
        // 6.关闭通道 跟连接
        channel.close();
        connection.close();
    }
}
